// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
    env: cloud.DYNAMIC_CURRENT_ENV
})
const db = cloud.database()
const _ = db.command
const dbTask = db.collection('task')
const dbUser = db.collection('user')

// 云函数入口函数
exports.main = async (event, context) => {
    const wxContext = cloud.getWXContext()
    //更改任务状态为已领取
    if (event.action == 'taskReceived') {
        task = await dbTask.where({
            _id: event._id
        })
        taskInfo = await task.get()
        if (taskInfo['data'][0]['state'] !== 0){
            return 'error'
        }
        var receiver = await dbUser.where({
            openid: event.openid
        })
        await receiver.update({
            data:{
                taskListReceive: _.push([event._id])
            }
        })
        receiverInfo = await receiver.get()
        return await dbTask.where({
            _id: event._id
        }).update({
            data: {
                state: 1,
                receiverInfo: {
                    openid:receiverInfo['data'][0]['openid'],
                    avatarUrl:receiverInfo['data'][0]['avatarUrl'],
                    nickName:receiverInfo['data'][0]['nickName'],
                    contactType:event.contactType,
                    contactNumber:event.contactNumber
                }
            }
        })
    }
    //更改任务状态为已完成
    else if (event.action == 'taskCompleted') {
        await dbTask.where({
            _id: event._id
        }).update({
            data: {
                state: 2
            }
        })
        //增加完成者的积分
        let task=await dbTask.where({
            _id:event._id
        }).get()
        return await dbUser.where({
            openid: task['data'][0]['receiverInfo']['openid']
        }).update({
            data:{
                credit: _.inc(task['data'][0]['price'])
            }
        })
    }
    //取消任务的发布
    else if (event.action == 'taskCancelPublish') {
        let task=await dbTask.where({
            _id:event._id
        }).get()
        await dbUser.where({
            openid: event.openid
        }).update({
            data:{
                taskListPublish: _.pull(event._id),
                //增加取消发布者的积分
                credit: _.inc(task['data'][0]['price'])
            }
        })
        return await dbTask.where({
            _id: event._id
        }).remove()
    }
    //取消任务的领取
    else if (event.action == 'taskCancelReceive') {
        await dbUser.where({
            openid: event.openid
        }).update({
            data:{
                taskListReceive: _.pull(event._id)
            }
        })
        return await dbTask.where({
            _id: event._id
        }).update({
            data: {
                state: 0,
                receiverInfo: {
                    nickName: '',
                    openid: '',
                    avatarUrl: '',
                    contactType:'',
                    contactNumber:''
                },
            }
        })
    }
    //更新任务信息
    else if (event.action == 'taskUpdate' && event.task) {
        return await dbTask.where({
            _id: event._id
        }).update({
            data: {
                content: event.task.content,
                from: event.task.from,
                to: event.task.to,
                price: event.task.price
            }
        })
    }
    //添加任务
    else if (event.action == 'taskPublished' && event.task) {
        var publisher = await dbUser.where({
            openid: event.openid
        })
        var publisherInfo = await publisher.get()
        if(publisherInfo['data'][0]['credit']<event.task.price){
            return false
        }
        const ret = await dbTask.add({
            data: {
                content: event.task.content,
                from: event.task.from,
                to: event.task.to,
                price: event.task.price,
                state: 0,    
                receiverInfo: {
                    nickName: '',
                    openid: '',
                    avatarUrl: '',
                    contactType:'',
                    contactNumer:''
                },
                publisherInfo:{
                    openid:publisherInfo['data'][0]['openid'],
                    avatarUrl:publisherInfo['data'][0]['avatarUrl'],
                    nickName:publisherInfo['data'][0]['nickName'],
                    contactType:event.contactType,
                    contactNumber:event.contactNumber
                }
            }
        })
        //扣除发布者积分
        await dbUser.where({
            openid: event.openid
        }).update({
            data:{
                credit: _.inc(-event.task.price)
            }
        })
        return await publisher.update({
            data:{
                taskListPublish: _.push([ret._id])
            }
        })
    }
}